package com.wwdx.service.impl;

import com.google.common.collect.Maps;
import com.wwdx.model.WwdxPersonalModel;
import com.wwdx.model.WwdxVipUserModel;
import com.wwdx.service.WwdxVipUserServiceI;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 */
@Service
public class WwdxVipUserServiceImpl extends BaseServiceImpl<WwdxVipUserModel> implements WwdxVipUserServiceI {

    @Override
    public Long selectModelCount(WwdxVipUserModel model) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String hql = "select count(1) from WwdxVipUserModel\t"+getParams(model,params);
        return count(hql,params);
    }

    private String getParams(WwdxVipUserModel model, Map<String, Object> params){
        StringBuilder sb = new StringBuilder("where 1=1\n");
        return sb.toString();
    }

    @Override
    public List<WwdxVipUserModel> selectModelList(WwdxVipUserModel model, int page,int rows) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String hql = "from WwdxVipUserModel\t"+getParams(model,params)+"\torder by gmt_created desc";
        List<WwdxVipUserModel> wwdxVipUserModels = find(hql, params, page, rows);
        wwdxVipUserModels.forEach(wwdxVipUserModel -> {
            WwdxPersonalModel wwdxPersonalModel = wwdxPersonalService.getByUnionid(wwdxVipUserModel.getUnionid());
            if(wwdxPersonalModel != null){
                wwdxVipUserModel.setUserName(wwdxPersonalModel.getNickname());
            }else {
                wwdxVipUserModel.setUserName("用户不存在");
            }
        });
        return wwdxVipUserModels;
    }

    @Override
    public WwdxVipUserModel getByUnionId(String unionid) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("unionid",unionid);
        return getByHql("from WwdxVipUserModel where unionid =:unionid",params);
    }
}